﻿using System.IO;
using static System.Windows.Forms.LinkLabel;

namespace showcsv
{
    public class CsvReader
    {
        public static Bitmap ReadCsv(double[,] data, int cols, int rows)
        {
            //找到最大值和最小值
            double min = double.MaxValue;
            double max = double.MinValue;

            foreach (double value in data)
            {
                if (value < min) min = value;
                if (value > max) max = value;
            }

            Bitmap bmp = new Bitmap(cols, rows);
            for (int x = 0; x < rows; x++)
            {
                for (int y = 0; y < cols; y++)
                {
                    int value = Convert.ToInt32((data[x, y] - min) / (max - min) * 255);
                    //if(value < 0) value = 0;
                    Color color = Color.FromArgb(value, value, value);
                    bmp.SetPixel(y, x, color);
                }

            }
            return bmp;
        }

        public static String CalculateAverageBrightness(double[,] data, int cols, int rows, int proportion)
        {
            Dictionary<int, double> proportionFactors = new Dictionary<int, double>() {
                {10, 3.162 },{15, 2.582},{20, 2.236},{25,2.0},{30, 1.826},{35, 1.690},{40,1.581},{50, 1.414},{60,1.291},{70,1.195},{80,1.118},
                {90,1.054},{100,1.0}
            };
            double proportionFactor = proportionFactors[proportion];
            int rowStart = System.Convert.ToInt32((rows - rows / proportionFactor) / 2);
            int rowEnd = rows - System.Convert.ToInt32((rows - rows / proportionFactor) / 2);
            int colStart = System.Convert.ToInt32((cols - cols / proportionFactor) / 2);
            int colEnd = cols - System.Convert.ToInt32((cols - cols / proportionFactor) / 2);
            MessageBox.Show("the rowStart: " + System.Convert.ToInt32(rowStart) + "--the rowEnd: " + System.Convert.ToInt32(rowEnd));
            MessageBox.Show("the colStart: " + System.Convert.ToInt32(rowStart) + "--the rowEnd: " + System.Convert.ToInt32(rowEnd));
            double averageBrightness = 0;
            string result;
            for (int x = rowStart; x < rowEnd; x++)
            {
                for (int y = colStart; y < colEnd; y++)
                {
                    averageBrightness += data[x, y];
                }
            }
            averageBrightness = averageBrightness / (rowEnd - rowStart) / (colEnd - colStart);
            result = "平均亮度: " + (averageBrightness).ToString() + " --colStart: " + colStart + " --colEnd: " + colEnd + " --rowStart: " + rowStart + " --rowEnd: " + rowEnd;

            return result;

        }

        public static String CalculateLocalBrightness(double[,] data, int cols, int rows, String proportion)
        {
            String s="hello";
            Char direction = proportion[0];
            int scale = System.Convert.ToInt32(proportion.Substring(proportion.Length-2,2));
            int rowStart = 0;
            int colStart = 0;
            int rowEnd = 0;
            int colEnd = 0;
            switch (direction)
            {
                case 'L':
                    colStart = 0;
                    colEnd = cols * scale / 100;
                    rowStart = 0;
                    rowEnd = rows;
                    break;
                case 'R':
                    colStart = cols - cols*scale/100;
                    colEnd = cols;
                    rowStart = 0;
                    rowEnd = rows;
                    break;
                case 'T':
                    colStart = 0;
                    colEnd=cols;
                    rowStart = 0;
                    rowEnd = rows*scale/100; 
                    break;
                case 'B':
                    colStart = 0;
                    colEnd = cols;
                    rowStart = rows - rows*scale/100;
                    rowEnd = rows;
                    break;


            }
            MessageBox.Show("the rowStart: " + System.Convert.ToInt32(rowStart) + "--the rowEnd: " + System.Convert.ToInt32(rowEnd));
            MessageBox.Show("the colStart: " + System.Convert.ToInt32(rowStart) + "--the rowEnd: " + System.Convert.ToInt32(rowEnd));
            double averageBrightness = 0;
            string result;
            for (int x = rowStart; x < rowEnd; x++)
            {
                for (int y = colStart; y < colEnd; y++)
                {
                    averageBrightness += data[x, y];
                }
            }
            averageBrightness = averageBrightness / (rowEnd - rowStart) / (colEnd - colStart);
            result = "平均亮度: " + (averageBrightness).ToString() + " --colStart: " + colStart + " --colEnd: " + colEnd + " --rowStart: " + rowStart + " --rowEnd: " + rowEnd;

            return result;
        }
    }

}
